<?php

class ModelStoryCategory extends Model {
      
      /**
       * 获取总条数
       */
      public function count($categoryId = 0) {
            $sql = sprintf("SELECT count(0) AS num FROM `%s` WHERE `status` = '1'",
                  "story_category"
            );
            if ($categoryId > 0) {
                  $sql = sprintf("SELECT count(0) AS num FROM `%s` AS sc 
                        INNER JOIN `%s` AS s ON sc.story_id = s.story_id AND sc.page_id = 1
                        WHERE s.category_id = '%d' ",
                        "story_content",
                        "story",
                        (int)$categoryId
                  );
            }
            $query = $this->db->query($sql);
            if ($row = $query->row) {
                  return $row['num'];
            }
            return 0;
      }

      public function list() {
            $query = $this->db->query(sprintf("SELECT * FROM `%s` WHERE `status` = '1' ORDER BY `sort_order` ASC",
                  "story_category"
            ));
            return $query->rows;
      }
}